package com.tarena.csmall.front.web.service;

import cn.tedu.mall.pojo.front.dto.ReduceSkuDTO;
import cn.tedu.mall.pojo.product.vo.SkuStandardVO;
import com.tarena.csmall.front.web.repository.SkuRepository;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SkuService {
    @Autowired
    private SkuRepository skuRepository;
    public List<SkuStandardVO> getSkus(Long id) {
        return skuRepository.getSkus(id);
    }

    public List<SkuStandardVO> batchGetSkus(List<Long> ids) {
        return skuRepository.batchGetSkus(ids);
    }

    /**
     * 减库存 要考虑还库存
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean reduceStock(List<ReduceSkuDTO> dtos) {
        //1.执行减库存操作 每减一次库存,验证是否成功,没成功抛出异常,由事务实现回滚
        skuRepository.reduceSotck(dtos);
        //2.如果全部扣减成功,保留操作的日志记录,防止减库存成功,但是新增订单失败,补偿操作
        skuRepository.reduceLogRecord(dtos);
        return true;
    }
}
